package com.uikoo9.manage.y.util;

import com.jfinal.plugin.activerecord.Page;

/**
 * Created with IntelliJ IDEA.
 * User: shoko (fbjia.com)
 * Date: 13-11-18
 * Time: 下午5:55
 * To change this template use File | Settings | File Templates.
 */
public class PagerUtil {
    public Page page;
    public String action;

    public PagerUtil(Page p,String action){
        this.page= p;
        this.action = action;
    }

    public String getSummary(){
        return "总"+page.getTotalRow()+"条记录,"+ page.getPageSize()+"/页";
    }

    public String getHref(){
        StringBuilder _href = new StringBuilder("");
        _href.append("<a href=\""+action+"-1\">首页</a>");
        int begin = _href.length();
        for(int i=1;i<=5 && (page.getPageNumber()-i) >0;i++){
            _href.insert(begin,"<a href=\""+action+"-"+(page.getPageNumber()-i)+"\"> "+(page.getPageNumber()-i)+" </a>");
            if( i ==5 && (page.getPageNumber()-i) > 1){
                _href.insert(begin,"...");
            }
        }
        _href.append("["+page.getPageNumber()+"]");
        for(int i=1;i<=5 && (page.getPageNumber()+i) <=page.getTotalPage();i++){
            _href.append("<a href=\""+action+"-"+(page.getPageNumber()+i)+"\"> "+(page.getPageNumber()+i)+" </a>");
            if( i ==5 && (page.getPageNumber()+i) < page.getTotalPage()){
                _href.append("...");
            }
        }
        _href.append("<a href=\""+action+"-"+page.getTotalPage()+"\">末页</a> [总"+page.getTotalPage()+"页]");
        return _href.toString();
    }

    public String getAll(){
        return "<div style='text-align:center;'>"+getSummary() +" ------------------------ "+getHref()+"</div>";
    }

}
